/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.startup.musicstore.services.Impl;

import com.startup.musicstore.domain.Album;
import com.startup.musicstore.domain.MarketingInformattion;
import com.startup.musicstore.domain.Review;
import com.startup.musicstore.domain.Track;
import com.startup.musicstore.respository.AlbumRepository;
import com.startup.musicstore.services.AlbumService;
import com.startup.musicstore.services.grade.AHandler;
import com.startup.musicstore.services.grade.BHandler;
import com.startup.musicstore.services.grade.CHandler;
import com.startup.musicstore.services.grade.DHandler;
import com.startup.musicstore.services.grade.GradeHandler;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Clive
 */
@Service
public class AlbumServicesImpl implements AlbumService{

    @Autowired
    private AlbumRepository albumRepository;
    
    @Override
    public List<Review> getAlbumRatings(Long id) {
        return albumRepository.findOne(id).getMarketingInformattion().getReview();
    }

    @Override
    public String getRating(int grade) {
        
        GradeHandler aHandler = new AHandler();
        GradeHandler bHandler = new BHandler();
        GradeHandler cHandler = new CHandler();
        GradeHandler dHandler = new DHandler();
        
        aHandler.setSuccessor(bHandler);
        bHandler.setSuccessor(cHandler);
        cHandler.setSuccessor(dHandler);
        
        return aHandler.handleGrade(grade);
    }

    @Override
    public Album find(Long id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Album persist(Album entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Album merge(Album entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void remove(Album entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Album> findAll() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String getTrackUrl(Long albumId, int trackNumber) {
       Album a = albumRepository.findOne(albumId);
       List<Track> tracks = a.getTracks();
       String url = "";
       for(Track t : tracks)
       {
           if(t.getTrackNumber() == trackNumber)
           {
               url = t.getSampleClip().getUrl();
           }
       }
       return url;
    }

    @Override
    public void updateInventory(Long albumId, int quantity) {
        Album a = albumRepository.findOne(albumId);
        a.setInventory(a.getInventory() - quantity);
        a.setSales(a.getSales() + quantity);
    }

    @Override
    public void postRating(Long album, Review review) throws Exception{
        Album a = albumRepository.findOne(album);
        MarketingInformattion information = a.getMarketingInformattion();
        
        if(information.getStatusOfInformation().equals("PENDING"))
        {
            throw new Exception("The current status is pending.");
        }
        else
        {
            List<Review> reviews = information.getReview();
            reviews.add(review);
            information.setReview(reviews);
            a.setMarketingInformattion(information);
        }
    }
    
}
